var host = "https://w.daoker.club/"
const formatTime = date => {

    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}
const alertmodel = (title, content) => {
    wx.showModal({
        title: title,
        content: content,
        success(res) {
            if (res.confirm) {
                console.log('用户点击确定')
            } else if (res.cancel) {
                console.log('用户点击取消')
            }
        }
    })
}
const getUserVipInfo = (url, data, doSuccess) => {
    wx.request({
        url: host + url,
        data: data,
        header: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        method: 'post',
        success: function(res) {
            // console.log(res)
            doSuccess(res)
        }
    })
}
const imgHeight = (e, doSuccess) => { //图片计算高度方法
    var imgwidth = e.detail.width,
        imgheight = e.detail.height,
        ratio = imgwidth / imgheight;
    doSuccess(ratio)
}
const getRequest = (url, data, doSuccess) => { // get请求封装
    wx.request({
        url: host + url,
        data: data,
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function(res) {
            // success

            doSuccess(res.data)
        }
    })
}
const setStorage = (key, value) => {
    wx.setStorage({
        key: key,
        data: value
    })
}
const getStorage = (key, doSuccess) => {
    wx.getStorage({
        key: key,
        success: function(res) {
            doSuccess(res.data)
        }
    })
}
const postRequest = (url, data, doSuccess) => { //post请求封装
    wx.request({
        url: host + url,
        data: data,
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }, // 设置请求的 header
        success: function(res) {
            // success
            console.log(url, data)
            doSuccess(res.data)
        }
    })
}
const getAdvert = (data, doSuccess) => {
    wx.request({
        url: host + 'FinanceManage/rest/advertList.do',
        data: data,
        header: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        method: 'post',
        dataType: 'json',
        responseType: 'text',
        success: (res) => {
            doSuccess(res.data)
        }
    });
}
const alertModel = (data, doSuccess) => {
    wx.showModal({
        title: data.title,
        content: data.text,
        success(res) {
            if (res.confirm) {
                doSuccess(res)
            } else if (res.cancel) {
                doSuccess(res)
            }
        }
    })
}
module.exports = {
    formatTime: formatTime,
    alertmodel: alertmodel,
    imgHeight: imgHeight,
    getRequest: getRequest,
    postRequest: postRequest,
    getAdvert: getAdvert,
    alertModel: alertModel,
    getUserVipInfo: getUserVipInfo,
    setStorage: setStorage,
    getStorage: getStorage
}